package com.selune.online.ordering.system.common.advice;

import com.selune.online.ordering.system.project.api.base.response.Response;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常拦截
 *
 * @author xiaoyp
 * @date 2020/3/2
 */

@RestControllerAdvice
public class GlobalExceptionHandlerAdvice {
	
	private static final int HTTP_ERROR_CODE = 400;
	private static final String ERROR_MESSAGE = "请求失败";
	
	@ExceptionHandler(Exception.class)
	public Response<Object> defaultExceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
		
		String error = e.getCause().getMessage();
		String path = httpServletRequest.getRequestURI();
		
		return Response.error(HTTP_ERROR_CODE, ERROR_MESSAGE, error, null, path);
	}
}
